// var House = House || {};
/**
 * gTypeId:lwall
 * goodsName:wall;
 */
House = function(camera, scene, stageSize) {
	this.camera = camera;
	this.scene = scene;
	this.wallHeight = RoomUtil.wallHeight;
	this.wallThick = RoomUtil.wallThick;
	this.facility = new Facility(scene, camera, stageSize);
	this.sHouse = {
		hsid : null,
		uid : null,
		rooms : null
	};
	this.rTypePoisitons = [];
	this.wallSelects = [];

	this.roomStructures = [];
	
	this.roomFloorGroup=new THREE.Object3D;
	this.roomFloorGroup.name='roomGroup';
	this.scene.add(this.roomFloorGroup);
}
House.prototype.constructor = House;

House.prototype.createFloor = function(position, size,roomType) {
	var mat = new THREE.MeshPhongMaterial();
	var geo = new THREE.CubeGeometry(size.x, size.y, this.wallThick,5,5,5);
	var id = RoomUtil.random();
	var styleMesh = this.facility.loadStyle(geo, 1, position, new THREE.Vector3, mat, "group", "roomTypeName", id, "structure", 0, "structure", roomType);
	styleMesh.union = false;
	this.facility.paintSingle(styleMesh, RoomUtil.INIT_FLOOR_TEXTURE, 1);
  return styleMesh;
};

House.prototype.showStructure = function(position, size,roomType) {
	var mat = new THREE.MeshPhongMaterial();
	var geo = new THREE.CubeGeometry(size.x, this.wallThick, size.z);
	var id = RoomUtil.random();
	var styleMesh = this.facility.loadStyle(geo, 1, position, new THREE.Vector3, mat, "subStructure", "roomTypeName", id, "structure", 0, "structure", roomType);
	styleMesh.union = false;
	this.facility.paintSingle(styleMesh, RoomUtil.INIT_FLOOR_TEXTURE, 1);
  return styleMesh;
};

House.prototype.createWall=function(wallDetailGTypeId,wallDetailSize,wallDetailPosition){

  	var mat = new THREE.MeshBasicMaterial();
	var geo = new THREE.CubeGeometry(wallDetailSize.x, wallDetailSize.y,wallDetailSize.z);
	// position.z+= this.wallHeight / 2;
	var id = RoomUtil.random();
	var styleMesh = this.facility.loadStyle(geo, 1, wallDetailPosition, new THREE.Vector3, mat, "room", "roomTypeName", id, wallDetailGTypeId, 0, "wall", "roomType");
	styleMesh.union = false;
	this.scene.add(styleMesh);
	if(wallDetailGTypeId=='xwall'
	||wallDetailGTypeId=='fwall'||wallDetailGTypeId=='bwall'
	||wallDetailGTypeId=='rwall'||wallDetailGTypeId=='lwall'){
		// this.facility.paintSingle(styleMesh, RoomUtil.INIT_WALL_TEXTURE, 1);
		this.facility.paintSingle(styleMesh, "0xf0f1f1", 0);
				
	}else if (wallDetailGTypeId=='ywall'){
		this.facility.paintSingle(styleMesh, "0xf0f1f1", 0);
		// this.facility.paintSingle(styleMesh, RoomUtil.INIT_WALL_TEXTURE, 1);
	}
	else{
		this.facility.paintSingle(styleMesh, RoomUtil.INIT_FLOOR_TEXTURE, 1);
	}

  return styleMesh;
}
House.prototype.createWalls = function() {

$.each(build.houseMeshList,function(i,n){
    console.warn(n);
});
	for(var i = 0; i < this.scene.children.length; i++) {
		var floor = this.scene.children[i];

		if(floor.gTypeId == "floor") {
			var hasWallFlg = {
				"f" : false,
				"b" : false,
				"l" : false,
				"r" : false
			};

			var position = new THREE.Vector3;
			position.x = floor.position.x;
			position.y = floor.position.y;
			position.z = this.wallThick / 2;
			var size = floor.size;

			var fwallPosition = new THREE.Vector3(position.x, position.y + size.y / 2 , position.z + this.wallHeight / 2);
			var fwallSize = {
				"x" : size.x- this.wallThick,
				"y" : this.wallThick,
				"z" : this.wallHeight
			};
			var fwall = {
				"position" : fwallPosition,
				"size" : fwallSize
			};
			var bwallPosition = new THREE.Vector3(position.x, position.y - size.y / 2 , position.z + this.wallHeight / 2);
			var bwallSize = {
				"x" : size.x- this.wallThick,
				"y" : this.wallThick,
				"z" : this.wallHeight
			};
			var bwall = {
				"position" : bwallPosition,
				"size" : bwallSize
			};
			var rwallPosition = new THREE.Vector3(position.x + size.x / 2 , position.y, position.z + this.wallHeight / 2);
			var rwallSize = {
				"x" : this.wallThick,
				"y" : size.y- this.wallThick,
				"z" : this.wallHeight
			};
			var rwall = {
				"position" : rwallPosition,
				"size" : rwallSize
			};

			var lwallPosition = new THREE.Vector3(position.x - size.x / 2 , position.y, position.z + this.wallHeight / 2);
			var lwallSize = {
				"x" : this.wallThick,
				"y" : size.y- this.wallThick,
				"z" : this.wallHeight
			};
			var lwall = {
				"position" : lwallPosition,
				"size" : lwallSize
			};

			for(var j = 0; j < this.scene.children.length; j++) {
				var wall = this.scene.children[j];
				if(wall.goodsName == "wall") {
					if(RoomUtil.checkRectIntersectOrUnion(wall, fwall)==1) {
						hasWallFlg.f = true;
					}
					if(RoomUtil.checkRectIntersectOrUnion(wall, bwall)==1) {
						hasWallFlg.b = true;
					}
					if(RoomUtil.checkRectIntersectOrUnion(wall, lwall)==1) {
						hasWallFlg.l = true;
					}
					if(RoomUtil.checkRectIntersectOrUnion(wall, rwall)==1) {
						hasWallFlg.r = true;
					}
				}
			};
			// 	front wall
			if(!hasWallFlg.f) {

				var mat = new THREE.MeshBasicMaterial();
				var geo = new THREE.CubeGeometry(fwallSize.x, fwallSize.y, fwallSize.z);
				var fwallMesh = this.facility.loadStyle(geo, 1, fwallPosition, new THREE.Vector3, mat, "room", "roomTypeName",  RoomUtil.random(), "fwall", 0, "wall", "roomType");
				this.facility.paintSingle(fwallMesh, "obj/wall/water2.jpg", 1);
				alert('front');
			}
			// 	back wall
			if(!hasWallFlg.b) {

				var mat = new THREE.MeshBasicMaterial();
				var geo = new THREE.CubeGeometry(bwallSize.x, bwallSize.y, bwallSize.z);
				var styleMesh = this.facility.loadStyle(geo, 1, bwallPosition, new THREE.Vector3, mat, "room", "roomTypeName",  RoomUtil.random(), "bwall", 0, "wall", "roomType");
				this.facility.paintSingle(styleMesh, "obj/wall/water2.jpg", 1);
				alert('back');
			}

			// 	right wall
			if(!hasWallFlg.r) {
				var mat = new THREE.MeshBasicMaterial();

				var geo = new THREE.CubeGeometry(rwallSize.x, rwallSize.y, rwallSize.z);
				var styleMesh = this.facility.loadStyle(geo, 1, rwallPosition, new THREE.Vector3, mat, "room", "roomTypeName",  RoomUtil.random(), "rwall", 0, "wall", "roomType");
				this.facility.paintSingle(styleMesh, "obj/wall/water2.jpg", 1);
				alert('right');
			}
			// 	left wall
			if(!hasWallFlg.l) {
				var mat = new THREE.MeshBasicMaterial(lwallSize.x, lwallSize.y, lwallSize.z);
				var geo = new THREE.CubeGeometry(this.wallThick, size.y, this.wallHeight);
				var styleMesh = this.facility.loadStyle(geo, 1, lwallPosition, new THREE.Vector3, mat, "room", "roomTypeName",  RoomUtil.random(), "lwall", 0, "wall", "roomType");
				this.facility.paintSingle(styleMesh, "obj/wall/water2.jpg", 1);
				alert('left');
			}
		}

	}
};
// House.prototype.createWalls = function(position, size) {
// var walls = [];
// /*
// 矩形各由两点(左上/右下)决定，使用屏幕坐标系
// 矩形   A   (x1,y1),(x2,y2);
// 矩形   B   (x3,y3),(x4,y4);
//
// 设   m=   (x1> x4)|(x2 <x3)
// n=   (y2 <y3)|(y1> y4)
//
// if(m|n)
// {
// //不相交
// }
// else
// {
// //相交
// } */
//
// for(var i = 0; i < this.roomStructures.length; i++) {
// for(var j = 0; j < this.roomStructures[i].walls.length; j++) {
// rsw = this.roomStructures[i].walls[j];
// if(rsw.type == "floor") {
// var ax1 = rsw.position.x - rsw.size.x / 2;
// var ay1 = rsw.position.y - rsw.size.y / 2;
// var ax2 = rsw.position.x + rsw.size.x / 2;
// var ay2 = rsw.position.y + rsw.size.y / 2;
// var bx1 = position.x - size.x / 2;
// var by1 = position.y - size.y / 2;
// var bx2 = position.x + size.x / 2;
// var by2 = position.y + size.y / 2;
// var m = (ax1 >= bx2) | (ax2 <= bx1);
// var n = (ay2 <= by1) | (ay1 >= by2);
// if(!(m | n)) {
// return;
// } else {
// break;
// }
//
// }
// }
// }
//
// //put the wall data into room;
//
// // 	floor
// var wall = {
// "type" : "floor",
// "size" : {
// "x" : size.x,
// "y" : size.y,
// "z" : this.wallThick
// },
// "position" : {
// "x" : position.x,
// "y" : position.y,
// "z" : position.z
// }
// };
// walls.push(wall);
// // 	front
// wall = {
// "type" : "front",
// "size" : {
// "x" : size.x,
// "y" : this.wallThick,
// "z" : this.wallHeight
// },
// "position" : {
// "x" : position.x,
// "y" : position.y + size.y / 2 - this.wallThick / 2,
// "z" : position.z + this.wallHeight / 2
// }
// };
// walls.push(wall);
// //	back
// wall = {
// "type" : "back",
// "size" : {
// "x" : size.x,
// "y" : this.wallThick,
// "z" : this.wallHeight
// },
// "position" : {
// "x" : position.x,
// "y" : position.y - size.y / 2 + this.wallThick / 2,
// "z" : position.z + this.wallHeight / 2
// }
// };
// walls.push(wall);
// // 	right
// wall = {
// "type" : "right",
// "size" : {
// "x" : this.wallThick,
// "y" : size.y,
// "z" : this.wallHeight
// },
// "position" : {
// "x" : position.x + size.x / 2 - this.wallThick / 2,
// "y" : position.y,
// "z" : position.z + this.wallHeight / 2
// }
// };
// walls.push(wall);
// // left
// wall = {
// "type" : "left",
// "size" : {
// "x" : this.wallThick,
// "y" : size.y,
// "z" : this.wallHeight
// },
// "position" : {
// "x" : position.x - size.x / 2 + this.wallThick / 2,
// "y" : position.y,
// "z" : position.z + this.wallHeight / 2
// }
// };
// walls.push(wall);
//
// var roomStructure = {
// "roomType" : 1,
// "walls" : walls
// };
//
// this.roomStructures.push(roomStructure);
//
// //draw the room's wall.
// // 	floor
// var mat = new THREE.MeshBasicMaterial();
// var geo = new THREE.CubeGeometry(size.x, size.y, this.wallThick);
// var styleMesh = this.facility.loadStyle(geo, 1, position, new THREE.Vector3, mat, "room", "roomTypeName", "wall", "floor", 0, "floor", "roomType");
// this.facility.paintSingle(styleMesh, "obj/wall/water2.jpg", 1);
//
// // styleMesh.rotation.x = -90 * Math.PI / 180;
// // 	front wall
// var mat = new THREE.MeshBasicMaterial();
// var wallPosition = new THREE.Vector3(position.x, position.y + size.y / 2 - this.wallThick / 2, position.z + this.wallHeight / 2);
// var geo = new THREE.CubeGeometry(size.x, this.wallThick, this.wallHeight);
// var fwallMesh = this.facility.loadStyle(geo, 1, wallPosition, new THREE.Vector3, mat, "room", "roomTypeName", "wall", "fwall", 0, "fwall", "roomType");
// this.facility.paintSingle(fwallMesh, "obj/wall/water2.jpg", 1);
//
// // 	back wall
// var mat = new THREE.MeshBasicMaterial();
// var wallPosition = new THREE.Vector3(position.x, position.y - size.y / 2 + this.wallThick / 2, position.z + this.wallHeight / 2);
// var geo = new THREE.CubeGeometry(size.x, this.wallThick, this.wallHeight);
// var styleMesh = this.facility.loadStyle(geo, 1, wallPosition, new THREE.Vector3, mat, "room", "roomTypeName", "wall", "bwall", 0, "bwall", "roomType");
// this.facility.paintSingle(styleMesh, "obj/wall/water2.jpg", 1);
// // 	right wall
//
// var mat = new THREE.MeshBasicMaterial();
// var wallPosition = new THREE.Vector3(position.x + size.x / 2 - this.wallThick / 2, position.y, position.z + this.wallHeight / 2);
// var geo = new THREE.CubeGeometry(this.wallThick, size.y, this.wallHeight);
// var styleMesh = this.facility.loadStyle(geo, 1, wallPosition, new THREE.Vector3, mat, "room", "roomTypeName", "wall", "rwall", 0, "rwall", "roomType");
// this.facility.paintSingle(styleMesh, "obj/wall/water2.jpg", 1);
//
// // 	left wall
//
// var mat = new THREE.MeshBasicMaterial();
// var wallPosition = new THREE.Vector3(position.x - size.x / 2 + this.wallThick / 2, position.y, position.z + this.wallHeight / 2);
// var geo = new THREE.CubeGeometry(this.wallThick, size.y, this.wallHeight);
// var styleMesh = this.facility.loadStyle(geo, 1, wallPosition, new THREE.Vector3, mat, "room", "roomTypeName", "wall", "lwall", 0, "lwall", "roomType");
// this.facility.paintSingle(styleMesh, "obj/wall/water2.jpg", 1);
//
// };
House.prototype.loadHouseStyle = function(hsId, roomStyles, wallTextures) {
	this.clear();
	console.warn(roomStyles);

	this.sHouse.hsid = hsId;
	var roomSelect = $('<select/>').addClass('sTP');
	roomSelect.append($('<option/>').attr('value',0).text("--请选择房间--"));
	$('.rTypePosition').append("进入：");
	$('.rTypePosition').append(roomSelect).append("<br>");
	$('.rTypePosition').append("观察：");

	var wallSelect = $('<select/>').addClass('sWP').append($('<option/>').attr('value',0).text("--请选择墙面--"));
	$('.rTypePosition').append(wallSelect);

	var roomTypeName = "";
	for(var i in roomStyles) {
		var roomId = roomStyles[i].roomId;
		var roomType = roomStyles[i].roomType;
		for(var v in roomStyles[i].structure) {
			var s = roomStyles[i].structure[v];
			var height = 0;
			var mat = null;
			var textureUrl = null;
			for(var t in wallTextures) {
				var wt = wallTextures[t];
				if(wt.roomId == roomId && wt.wallType == s.type) {
					textureUrl = wt.textureUrl;
					break;
				}
			}
			if(s.type == 'floor') {
				switch(roomType) {
					case "1":
						roomTypeName = "卧室";
						break;
					case "2":
						roomTypeName = "客厅";
						break;
					case "3":
						roomTypeName = "卫生间";
						break;
					case "4":
						roomTypeName = "厨房";
						break;
					case "5":
						roomTypeName = "走廊";
						break;
					case "6":
						roomTypeName = "储藏室";
						break;
					case "7":
						roomTypeName = "阳台";
						break;
					case "8":
						roomTypeName = "玄关";
						break;
				}
				roomSelect.append($('<option/>').attr('value',parseInt(s.x)+"|"+parseInt(s.y)+"|"+parseInt(s.z)+"|"+roomType).text(roomTypeName));
			}
			// wallSelect.append($('<option/>').attr('value',parseInt(s.x)+"|"+parseInt(s.y)+"|"+parseInt(s.z)).text(s.type));
			this.wallSelects.push({
				"rType" : roomType,
				"x" : s.x,
				"y" : s.y,
				"z" : s.z,
				"wType" : s.type
			});

			if(s.type == 'floor' || s.type == 'ceiling') {
				mat = new THREE.MeshBasicMaterial({
					color : 0x444444
				});
				height = this.wallThick;
			} else {
				mat = new THREE.MeshBasicMaterial({
					color : 0x556677
				});
				height = this.wallHeight;
			}
			mat = new THREE.MeshBasicMaterial();

			var geo = new THREE.CubeGeometry(parseInt(s.width), parseInt(height), parseInt(s.depth));

			// mesh = new THREE.Mesh(geo, mat);
			// mat.map=texture;
			// mesh.position.x=parseInt(s.x);
			// mesh.position.y=parseInt(s.y);
			// mesh.position.z=parseInt(s.z);
			// this.scene.add(mesh);
			if(s.type != 'ceiling') {
				var styleMesh = this.facility.loadStyle(geo, 1, new THREE.Vector3(parseInt(s.x), parseInt(s.y), parseInt(s.z)), new THREE.Vector3, mat, "room", roomTypeName, "wall", s.type, roomStyles[i].roomId, roomTypeName + ' ' + s.type, roomStyles[i].roomType);
				if(textureUrl != null) {
					this.facility.paintSingle(styleMesh, textureUrl, 1);
				} else {
					this.facility.paintSingle(styleMesh, RoomUtil.INIT_FLOOR_TEXTURE, 1);
				}

			}

		}

	}
	var me = this;
	roomSelect.change(function() {
		if(this.value == 0)
			return;
		var pos = this.value.split("|");

		me.camera.position.x = parseInt(pos[0]);
		// me.camera.position.y=parseInt(pos[1]);
		me.camera.position.z = parseInt(pos[2]);
		var rType = parseInt(pos[3]);
		var i = 0;
		for(var r in me.wallSelects) {
			selWall = me.wallSelects[r];
			if(selWall.rType == rType) {
				wallSelect.append($('<option/>').attr('value',parseInt(selWall.x)+"|"+parseInt(selWall.y)+"|"+parseInt(selWall.z)+"|"+selWall.wType).text(selWall.wType));
				i++;
				if(i > 5)
					break;
			}
		}
		wallSelect.change(function() {
			var p = this.value.split("|");
			var targetPosition = new THREE.Vector3();
			switch (p[3]) {
				case "floor":
					targetPosition.x = parseInt(p[0]);
					targetPosition.y = parseInt(p[1]) + 100 * Math.cos(-90);
					targetPosition.z = parseInt(p[2]);
					me.camera.lookAt(targetPosition);
					// targetPosition.z = position.z + 100 * Math.sin( 0 ) * Math.sin( this.theta );
					break;
				case "ceiling":
					targetPosition.x = parseInt(p[0]);
					targetPosition.y = parseInt(p[1]) + 100 * Math.cos(90);
					targetPosition.z = parseInt(p[2]);
					me.camera.lookAt(targetPosition);
					break;
				case "lwall":
					targetPosition.x = parseInt(p[0]) + 100 * Math.cos(-90);
					targetPosition.y = parseInt(p[1]);
					targetPosition.z = parseInt(p[2]);
					me.camera.lookAt(targetPosition);
					break;
				case "rwall":
					targetPosition.x =      parseInt(p[0]) - 100 * Math.cos(90);
					targetPosition.y = parseInt(p[1]);
					targetPosition.z = parseInt(p[2]);
					me.camera.lookAt(targetPosition);
					break;
				case "fwall":
					targetPosition.x = parseInt(p[0]);
					targetPosition.y = parseInt(p[1]);
					targetPosition.z = parseInt(p[2]) + 100 * Math.cos(90);
					me.camera.lookAt(targetPosition);
					break;
				case "bwall":
					targetPosition.x = parseInt(p[0]);
					targetPosition.y = parseInt(p[1]);
					targetPosition.z =      parseInt(p[2]) - 100 * Math.cos(-90);
					me.camera.lookAt(targetPosition);
					break;
			}

		});
	});
};
House.prototype.getFacility = function(event) {
	this.facility.setFocuse(event);

};

House.prototype.getStructure = function(event) {
	this.facility.setFocuseStructure(event);

};

House.prototype.gotoFacade=function(structure3D){
	// wallsDetails=structure3D.detail;
	var wallsDetails=structure3D.details;
	for (var i=0; i < wallsDetails.length; i++) {
		if(wallsDetails[i].gTypeId=='xwall'||wallsDetails[i].gTypeId=='ywall'){	
			var wall=this.createWall(wallsDetails[i].gTypeId,wallsDetails[i].size,wallsDetails[i].position);
			wall.roomId=structure3D.roomId;
		}
	  	
	};
	// this.scene.add(this.roomFloorGroup);
	// this.camera.position.y = 100;
	// this.camera.position.z = 500;
	//console.warn(wallsDetails);
};
House.prototype.setScene=function(scene){
	this.scene=scene;
	this.facility.scene=scene;
};
House.prototype.loadFacility = function(goodsGeo,goodsPosition,onMesh,facilityMeshList) {
	if(goodsGeo.gTypeId == '09') {
		// this.facility.load(goodsGeo);
		this.facility.paintSingle(this.facility.curMesh, goodsGeo.geometryPath, 1);
	} else
		this.facility.load(goodsGeo,goodsPosition,onMesh,facilityMeshList);

};


House.prototype.moveFacility=function(mouse2D){
	this.facility.move(mouse2D,this.facility.onMesh);
};
House.prototype.turnOn=function(lights){
  this.facility.turnOn(lights);
};
House.prototype.load = function(modHouse) {
	console.warn("house.load");
	me = this;
	$.post(loadHouseUrl, {
		h : modHouse
	}, function(data) {
		console.warn(data);

		me.loadHouseStyle(modHouse.hsid, data.roomStyles, data.wallTextures, true);
		faciStyle = data.faciStyle;
		for(i in faciStyle) {
			/**
			 * 需要增加获得宿主信息。hostFacInfo
			 */
			// hostFacInfo = {
			// rndId : faciStyle[i].rndId,
			// parasitizedFace : faciStyle[i].parasitizedFace
			// };

			me.facility.hostInfo = {
				"description" : faciStyle[i].fs.gid.description,
				"parasitizedFace" : faciStyle[i].fs.parasitizedFace,
				"rndId" : null,
				"gTypeId" : faciStyle[i].fs.gid.gTypeId,
				"name" : faciStyle[i].fs.gid.goodsName,
				"roomId" : faciStyle[i].roomId
			};
			pAndr = {
				"position" : faciStyle[i].fs.position,
				"rotation" : faciStyle[i].fs.rotation
			};
			me.facility.load(faciStyle[i].fs.gid, pAndr);
			me.facility.facMesh = null;
		}
	}, "json");
};
House.prototype.update = function() {
	this.facility.update();
};

House.prototype.removeFacility=function(){
	this.facility.remove();
}

House.prototype.clear = function() {
	var children = [];
	for(var c in this.scene.children) {

		var child = this.scene.children[c];
		var i = 1;
		console.warn(i);
		console.warn(child.name + ":" + child.gTypeId + ":" + child.roomId);
		i++;
		if(child.name == "room") {

			// return child;
			children.push(child);

		}
	}
	// this.scene.children=children;
	for(var cl in children) {
		var child = children[cl];
		this.scene.remove(child);
	}
	$('.rTypePosition').html('');
	$('#facilityPanel').html('');
};
House.prototype.save = function(saveHouseUrl) {
	// alert(eval(this.houseSession[0].rooms));
	//$.post(saveHouseUrl,{house:eval(this.houseSession),houseId:this.hsid},
	var sFacilities = [];
	var sTextures = [];
	var sRooms = [];
	var roomCount = 1, roomPosition = null, roomSize = null;

	for(var c in this.scene.children) {
		var child = this.scene.children[c];
		if(( typeof (child.roomId) != "undefined" || child.roomId != null )) {
			if(child._id != 'wall') {
				console.warn(child.name + ";" + child.roomId + ";" + child.gTypeId + ";" + child._id);
				var sFacility = {
					"gid" : child._id,
					"position" : {
						"x" : child.position.x,
						"y" : child.position.y,
						"z" : child.position.z
					},
					"rotation" : {
						"x" : child.rotation.x,
						"y" : child.rotation.y,
						"z" : child.rotation.z
					},
					"parasitizedFace" : child.parasitizedFace,
					"roomId" : parseInt(child.roomId)
				};
				sFacilities.push(sFacility);
			} else {
				var sTexture = {
					"textureUrl" : child.gid,
					"roomId" : parseInt(child.roomId),
					"wallType" : child.gTypeId
				};
				sTextures.push(sTexture);

				if(child.gTypeId == 'floor') {
					sRooms.push({
						"roomId" : roomCount++,
						"roomType" : child.roomType,
						"position" : {
							"x" : child.position.x,
							"z" : child.position.z
						},
						"size" : {
							"width" : child.size.x,
							"depth" : child.size.z
						},
						"facilities" : [],
						"textures" : []
					});
				}
			}
		}
	}

	if(sFacilities.length != 0 || sTextures.length != 0) {

		for(var r in sRooms) {

			for(var f in sFacilities) {
				if(sFacilities[f].roomId == parseInt(r) + 1) {
					sRooms[r].facilities.push(sFacilities[f]);
				}
			}

			for(var t in sTextures) {
				if(sTextures[t].roomId == parseInt(r) + 1) {
					sRooms[r].textures.push(sTextures[t]);
				}
			}

		}

		this.sHouse.rooms = sRooms;
		console.warn(this.sHouse);
		$.post(saveHouseUrl, {
			"hSession" : this.sHouse
		}, function(data) {
			console.log(data);
			// $('body').html(data);
		}, "json");
	} else {
		alert('must add the facility');
	}

};
